package com.litign.personcontrol.controller;

import com.litign.personcontrol.pojo.Result;
import com.litign.personcontrol.utils.AliOSSUtils;
import lombok .extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

//本地文件存储
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
//
//        System.out.println("文件上传"+username+"age"+age+"image"+image.getOriginalFilename());
////        log.info("文件上传：{},{},{}", username, age, image);
//
//        //获取文件上传原始名
//        String originalFilename = image.getOriginalFilename();
//
//        //构造唯一文件名字
//        int index = originalFilename.lastIndexOf(".");
//        String extname = originalFilename.substring(index);
//        String newFileName = UUID.randomUUID().toString() + extname;
////        log.info("新的文件名：{}", newFileName);
//        System.out.println("新的文件名"+newFileName);
//
//        //将文件存在磁盘目录当中E:images
//        image.transferTo(new File("X:\\Coding\\Javas\\images\\"+newFileName));
//
//        return Result.success(newFileName);
//    }

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        System.out.println("文件上传，文件名{}"+ image.getOriginalFilename());

        //调用阿里云的oss工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        System.out.println("文件上传完成，路径为"+url);

        return Result.success(url);
    }

}
